//
//  ProductDetailViewController.swift
//  BP_NewClient
//
//  Created by letv on 15/7/5.
//  Copyright (c) 2015年 normal. All rights reserved.
//  产品详情 Swift版本

import UIKit

// 定义回调函数
typealias ProductDetailCallBackBlock = (callBackStr : String) -> Void

class ProductDetailViewController: BPRootViewController , UIScrollViewDelegate ,UIActionSheetDelegate{
    
    @IBOutlet var headerView : UIView?
    @IBOutlet var headerSectionView : UIView?
    @IBOutlet var tableView: UITableView?
    @IBOutlet var downOrderBtn: UIButton?

    
    var productModel: ProductModel?
    
    let cellIdentifier = "ProductDetailTableViewCell"
    var productDetailBlock:ProductDetailCallBackBlock?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.navigationItem.title = "产品详情"
        self.navigationItem.leftBarButtonItem = self.getBackNavigationItem()
        self.navigationItem.rightBarButtonItem = self.getRightNavigationItem("product_collect", action: "collection")

        let tableViewRect = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: (UIScreen.mainScreen().bounds.height - 49 - 20))
        
        self.tableView?.frame = tableViewRect
        
        self.tableView?.tableHeaderView = headerView
        var storeModel : StoreModel
    }
    
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if (section == 0){
            headerSectionView?.frame = CGRectMake(0, 0, 320, 72)
            return headerSectionView
        }else{
            return nil
        }
    }
    
    func collection()
    {
        self.showNoticeMsg("收藏成功!")
    }
    
    // MARK: - Table view data source
    //返回节的个数
    func numberOfSectionsInTableView(tableView: UITableView) -> Int{
        return 3
    }
    //返回某个节中的行数
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (section == 0) {
            return 2
        }else if (section == 1){
            print("self.productModel!.content is \(self.productModel?.content)")
            if (self.productModel?.content != nil){
                return self.productModel!.content.count + 2
            }
        }else{
            if ((self.productModel?.purchase_info) != nil){
                return self.productModel!.purchase_info.count + 1;
            }
        }
        return 0;
    }
    
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if (section == 0) {
            return 72
        }else{
            return 0
        }
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
    {
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                return 45
            }else{
                return 75
            }
        }else if (indexPath.section == 1){
            if (indexPath.row == self.productModel!.content.count + 1) {
                return 80
            }else{
                return 44
            }
        }else{
            if(indexPath.row == 0){
                return 34
            }else{
                return 30
            }
        }
    }
    
    //为表视图单元格提供数据，该方法是必须实现的方法
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let identy = "ProductDetailTableViewCell"
        var mycell : ProductDetailTableViewCell?
        
        mycell  = tableView.dequeueReusableCellWithIdentifier(identy) as! ProductDetailTableViewCell?
        
        var nibs : NSArray = NSArray()
        if mycell == nil {
            nibs = NSBundle.mainBundle().loadNibNamed("ProductDetailTableViewCell", owner: nil, options: nil)
        }
        
        if indexPath.section == 0 {
 
            if indexPath.row == 0{
                mycell = nibs.lastObject as? ProductDetailTableViewCell
                return mycell!

            }else if indexPath.row == 1 {
                mycell = nibs.objectAtIndex(4) as? ProductDetailTableViewCell
                mycell?.telephone?.addTarget(self, action: "showTelephoneActionSheet", forControlEvents: UIControlEvents.TouchUpInside)
                return mycell!
            }

        }
        else if indexPath.section == 1 {
            
            if indexPath.row == 0 {
                mycell = nibs.firstObject as? ProductDetailTableViewCell
                return mycell!

            }else if indexPath.row == self.productModel!.content.count + 1 {
                
                print(self.productModel!.content.count)

                mycell = nibs.objectAtIndex(2) as? ProductDetailTableViewCell

                return mycell!
        
            }else {
      
                if (self.productModel?.content.count != 0){
                    let taoCanModel : TaoCanModel =  self.productModel!.content.objectAtIndex(indexPath.row-1) as! TaoCanModel
                    
                    mycell = nibs.objectAtIndex(1) as? ProductDetailTableViewCell
                    
//                    mycell?.name?.text = taoCanModel.name
                    mycell?.name?.text = "小胡闹"

                    mycell?.price?.text = taoCanModel.price
                    mycell?.amount?.text = taoCanModel.num
                    
                    return mycell!
                }
            }
        }
        else {
            if indexPath.row == 0 {
                mycell = nibs.objectAtIndex(5) as? ProductDetailTableViewCell
                return mycell!
            }
            else {
                if (self.productModel?.purchase_info.count > 0){
                    
                    mycell = nibs.objectAtIndex(3) as? ProductDetailTableViewCell
                    let taoCanModel : TaoCanModel =  self.productModel!.purchase_info.objectAtIndex(indexPath.row-1) as! TaoCanModel
                    print("indexPath.row is \(indexPath.row)", terminator: "")
                    mycell?.name?.text = taoCanModel.name
                    mycell?.infoTitle?.text = taoCanModel.value;
                    
                    return mycell!
                }
            }

        }
        
        return mycell!
    }
    
    // 弹出actionsheet
    func showTelephoneActionSheet(){
        let actionSheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: "125555")
        actionSheet.showInView(self.view)
    }
    
    func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int)
    {
        if buttonIndex == 0 {
            var url : NSURL? = NSURL(string: "tel:"+"15011036823")
            UIApplication.sharedApplication().openURL(url!)
        }
    }

    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
      
        let row = indexPath.row
        if row == 1 {
        
            let mapManager = MALocationManager.shareManager()
            mapManager.MASearchPath(self)
        }
    
    }
    
    @IBAction func downOrderBtnClicked()
    {
<<<<<<< HEAD
        var subMitOrder = SubmitOrderViewController()
        subMitOrder.productModel = self.productModel
=======
        let subMitOrder = SubmitOrderViewController()
<<<<<<< HEAD
        subMitOrder.productModel = self.productModel;
=======
>>>>>>> 288d846b1206e88894e02a6ccf2ace272f888b1f
>>>>>>> ceba347648d240cfcb94a979ed67bf23465e238d
        self.navigationController?.pushViewController(subMitOrder, animated: true)
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView)
    {
        if scrollView.contentOffset.y > 149
        {
//            headerSectionView?.frame.origin = CGPointMake(0,0)
//            self.view.addSubview(headerSectionView!)
//            println("headerSectionView is \(headerSectionView)")
        }
        else {

//            self.tableView?.reloadData()
        }

    }
    
    func addGetMoreFootView(){
    
    }

    
    // 地图位置
    func searchMapPath(){
//        var mapManager = MALocationManager.shareManager()
//        mapManager.MASearchPath(self)
    }

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
